<template>
    <div style="position: relative;height: 100%;">
        <a-spin size="large" tip="Loading..." style="position: absolute;top: 30%;left: 50%;"/>
    </div>
</template>

<script>
    import { grant } from "@/apis/upms";
    export default {
        props : {
            code : { type : String, required : true },
            flag : { type : String, required : true },
            callback : { type : String, required : true }
        },
        mounted() {
            let $this = this;
            grant($this.code,$this.flag)
                .then(res => {
                    //  保存访问令牌
                    $this.$store.commit('setAccessToken',res.data);
                    //  回到登陆前要访问的页面
                    $this.$router.push({ path : $this.callback }).then();
                }).catch(e => {
                    switch (e.code) {
                        //  需要绑定账号
                        case 305:
                            $this.$modal.warning({
                                title : e.code,
                                content : e.msg,
                                onOk : () => {
                                    //  摧毁全部弹窗
                                    $this.$modal.destroyAll();
                                    //  前往绑定
                                    $this.$router.push({
                                        name : 'binding',
                                        params : { user : e.data },
                                        query : { callback : encodeURI($this.callback) }
                                    }).then();
                                }
                            });
                            break;
                        //  询问是否需要重新获取授权码
                        case 307:
                            $this.$modal.confirm({
                                title : e.code,
                                content : '无效的授权码,重新获取授权码?',
                                onOk : () =>
                                    window.location.href = $this.$store.getters.getOauthAuthorizeUrl + '&callback=' + encodeURI($this.callback),
                                onCancel : () => {
                                    //  摧毁全部弹窗
                                    $this.$modal.destroyAll();
                                    //  回到首页
                                    $this.$router.push({name: 'index'}).then();
                                }
                            });
                            break;
                    }

                });
        }
    }
</script>
